package com.jgsu.farm.controller;

import com.jgsu.farm.entity.*;
import com.jgsu.farm.exception.DataAddException;
import com.jgsu.farm.exception.DataNotExistException;
import com.jgsu.farm.service.*;
import com.jgsu.farm.utils.CommonResult;
import com.jgsu.farm.utils.ResultCode;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Slf4j
@Api(value = "farm/sysuser/stresscategory/peststress", tags = "虫害胁迫类型信息管理模块")  //描述信息用的，标注在类上
@RestController
@RequestMapping("farm/sysuser/stresscategory/peststress")
public class PestStressController {
    @Autowired
    PestStressService pestStressService;


    /**
     * 虫害胁迫增加
     * */
    @ApiOperation(value = "虫害胁迫添加功能")
    @PostMapping(value = "/add", produces = "application/json")
    public CommonResult pestStressAdd(PestStress pestStress) {
        try {
            pestStressService.add(pestStress);
            log.info("虫害胁迫添加成功，虫害胁迫chineseName为：{}", pestStress.getChinesename());
            return CommonResult.success(pestStress);
        } catch (DataAddException e) {
            log.error("虫害胁迫添加失败，虫害胁迫chineseName为：{}", pestStress.getChinesename());
            throw new DataAddException(e.getCode(), e.getMessage());
        }
    }

    /**
     * 虫害胁迫删除
     * */
    @ApiOperation(value = "虫害胁迫删除功能")
    @DeleteMapping(value = "/delete", produces = "application/json")
    public CommonResult pestStressDelete(@RequestParam("pestid") Integer pestid) {
        try {
            pestStressService.delete(pestid);
            log.info("删除id为：{}的虫害胁迫信息",pestid);
            return CommonResult.success(ResultCode.SUCCESS);
        } catch (DataNotExistException ds) {
            log.error("不存在id为：{}的虫害胁迫信息，删除失败",pestid);
            throw new DataNotExistException(ds.getCode(), ds.getMessage());
        }
    }

    /**
     * 虫害胁迫查询
     */
    @ApiOperation(value = "虫害胁迫分页查询功能")
    @GetMapping(produces = "application/json")
    public CommonResult<PageInfo<PestStress>> pestStressList(@RequestParam(value = "page", required = false, defaultValue = "1") int page,
                                                             @RequestParam(value = "pagesize", required = false, defaultValue = "10") int pagesize) {
        try {
            PageInfo<PestStress> pestStressList = pestStressService.selectAll(page, pagesize);
            log.info("查询所有的虫害胁迫信息");
            return CommonResult.success(pestStressList);
        } catch (DataNotExistException e) {
            log.error("虫害胁迫信息表中没有数据");
            throw new DataNotExistException(e.getCode(), e.getMessage());
        }
    }


    /**
     * 虫害胁迫修改
     */
    @ApiOperation(value = "虫害胁迫修改功能")
    @PutMapping(value = "/update", produces = "application/json")
    public CommonResult pestStressUpdate(PestStress peststress) {
        try {
            pestStressService.update(peststress);
            log.info("更新chineseName为{}的虫害胁迫成功", peststress.getChinesename());
            return CommonResult.success(peststress);
        } catch (DataNotExistException e) {
            log.error("更新chineseName为{}的虫害胁迫失败", peststress.getChinesename());
            throw new DataNotExistException(e.getCode(), e.getMessage());
        }
    }
}
